package com.sgam.mf.web;

import java.security.Principal;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import com.sgam.mf.domain.ref.RefDomains;
import com.sgam.mf.domain.ref.RefGift;
import com.sgam.mf.domain.stock.StockDocument;
import com.sgam.mf.domain.stock.StockDocumentType;
import com.sgam.mf.domain.stock.StockOperation;
import com.sgam.mf.domain.stock.StockSold;
import com.sgam.mf.service.entity.ContractsService;
import com.sgam.mf.service.entity.RegCompaniesService;
import com.sgam.mf.service.entity.RegPersonsService;
import com.sgam.mf.service.ref.RefGiftService;
import com.sgam.mf.service.sec.SecUserService;
import com.sgam.mf.service.stock.StockDocumentService;
import com.sgam.mf.service.stock.StockDocumentTypeService;
import com.sgam.mf.service.stock.StockOperationService;
import com.sgam.mf.service.stock.StockSoldService;

@Controller
@RequestMapping("/stock")
public class StockController {

	protected final Logger log = Logger.getLogger(getClass());

	/* ****************************** REFERENCIES ****************************** */

	/* ****************************** REGISTRY ****************************** */
	@Autowired
	private RegPersonsService regPersonsService;
	@Autowired
	private RegCompaniesService regCompaniesService;

	/* ****************************** ENTITY ****************************** */

	@Autowired
	private ContractsService contractsService;
	
	@Autowired
	private StockDocumentTypeService stockDocumentTypeService;
	
	@Autowired
	private StockDocumentService stockDocumentService;
	
	@Autowired
	private StockOperationService stockOperationService;

	/* ****************************** SECURITY ****************************** */
	@Autowired
	private SecUserService secUserService;

	/* ****************************** SYSTEM ****************************** */

	/*
 * 
 */
	@Autowired
	RefGiftService refGiftService;
	
 	public List<RefGift> refGiftCBX() {
 	   log.debug("AppController - refGiftCBX");
     	return refGiftService.list(secUserService.getProxyCurrentUser()); 
   	} 
 	
    /* *********************************************************************************************************** */
    @RequestMapping("/listRefGiftCBX")
    public @ResponseBody List<RefGift>  listRefGiftCBX (WebRequest request, Principal principal, Locale locale) {
    	log.debug("JSONController - listRefGiftCBX ");
    	
        return  refGiftCBX();
    }  
 	
	@RequestMapping("/ping")
	public @ResponseBody
	String ping(WebRequest request, Principal principal, Locale locale) {
		log.debug("StockController - ping ");

		String result = "Hello from STOCK Controller";
		return result;
	}
	
	   /* *********************************************************************************************************** */
	 @RequestMapping("/stockDocumentTypeList")
	 public @ResponseBody List<StockDocumentType>  stockDocumentTypeList (WebRequest request, Principal principal, Locale locale) {
	 	log.debug("JSONController - stockDocumentTypeList ");
	
	 	List<StockDocumentType>   result = stockDocumentTypeService.list(secUserService.getProxyCurrentUser());
	 	return  result;
	 }	
	
	   /* *********************************************************************************************************** */
	 @RequestMapping("/stockDocumentList")
	 public @ResponseBody List<StockDocument>  stockDocumentList (WebRequest request, Principal principal, Locale locale) {
	 	log.debug("JSONController - stockDocumentList ");
	
	 	List<StockDocument>   result = stockDocumentService.list(secUserService.getProxyCurrentUser());
	 	
	 	Collections.sort(result);
	 	return  result;
	 } 
 

	   /* *********************************************************************************************************** */
    @RequestMapping("/stockOperationList")
    public @ResponseBody List<StockOperation>  stockOperationList (WebRequest request, Principal principal, Locale locale) {
    	log.debug("JSONController - stockOperationList ");

    	List<StockOperation>   result = stockOperationService.list(secUserService.getProxyCurrentUser());
    	return  result;
    } 
    
    @Autowired
    private StockSoldService  stockSoldService;
	   /* *********************************************************************************************************** */
	 @RequestMapping("/getStockSoldByForUserByDate")
	 public @ResponseBody List<StockSold>  getStockSoldByForUserByDate (@RequestParam(required=false) Integer 	userId, 
			 															@RequestParam(required=false) Date 		forDate) {
	 	log.debug("JSONController - getStockSoldByForUserByDate ");
	
	 	Integer pUserId 	= userId!=null?userId:0;
	 	Date    pForDate 	= forDate!=null?forDate:new Date();
	 	
	 	List<StockSold>   result = stockSoldService.getStockSoldByForUserByDate(pUserId, pForDate);
	 	return  result;
	 }    
 
   
	   /* *********************************************************************************************************** */
//	 @RequestMapping("/stockSoldList")
//	 public @ResponseBody List<StockSold>  stockSoldList () {
//	 	log.debug("JSONController - stockSoldList ");
//	
//	 	List<StockSold>   result = stockSoldService.list();
//	 	return  result;
//	 }  
     
	    @RequestMapping(value="/stockDocumentCardPopup", method = {RequestMethod.GET}) 
	    public String  stockDocumentCardPopup( Model m, 
	    									@RequestParam(value="docId", required=false ,defaultValue="-1") Integer docId) {
	    	log.debug("StockController - stockDocumentCardPopup - GET");
	    	log.debug("StockController - docId = " + docId);
	    	//m.addAttribute("p_docId", docId);
	    	StockDocument documentItem = new StockDocument();
	    	
	    	if (docId > -1) {
	    		documentItem =  stockDocumentService.get(docId, secUserService.getProxyCurrentUser());
	    	} else {
	    		documentItem.setDocumentComment("New Document");	
	    	}
	    	
	    	//m.addAttribute("refGiftCBX", refGiftCBX());
	    	m.addAttribute("documentItem", documentItem);	
	    	
	    	return "stockDocumentCardPopup";
	    }  
	    
//	    @RequestMapping(value="/clientiPFCardPopup", method = {RequestMethod.POST}) 
//	    public @ResponseBody RegPersons  clientiPFCardPopupPOST( Model m,  @ModelAttribute("personsItem") RegPersons item ) {
//	    	
//	    	log.debug("AppController - clientiPFCardPopup - POST");
//	    	
//	    	RegPersons personsItemResult = new RegPersons();
//	    	try {
//	        	item.setUpdatedByFk(secUserService.getCurrentUserId());
//	        	item.setUpdatedDate(new Date());
//	        	if(item.getRegCompanies().getId() == null || item.getRegCompanies().getId() == -1) {
//	        		item.setRegCompanies(null);
//	        	}
//	        	personsItemResult = regPersonsService.update(item, secUserService.getProxyCurrentUser());
//			} catch (Exception e) {
//				m.addAttribute("errorMessage", "Server Side Error Occured");
//				e.printStackTrace();
//			}
//	    	
//	    	return personsItemResult;
//	    }  	 
	 
	
}
